/*
*  NSRTMPredicate.m
*  RTMApiTest
*
*  Created by kkillian on 17/11/09.
*  Copyright 2009 shufflecodebox. All rights reserved.
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*
*	You should have received a copy of the GNU General Public License
*	along with this program; if not, write to the Free Software
*	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#import "NSRTMPredicate.h"

#define AND @"AND"
#define OR @"OR"

@implementation NSRTMPredicate

-(id)initWithPredicate:(NSRTMPredicate *)predicate {
	self = [super init];
	criteriacount = 0;
    if ( self ) {
		predicateValue = [[NSString alloc] initWithString:[predicate formattedPredicate]];
		criteriacount++;
    }
	
    return self;
}

-(id)initPredicateWithCriteria:(NSRTMCriteria *)criteria {
	self = [super init];
	criteriacount = 0;
    if ( self ) {
		predicateValue = [[NSString alloc] initWithString:[criteria getFormattedCriteria]];
		criteriacount++;
    }
	
    return self;
}

-(id)initWithQuery:(NSString *)query {
	self = [super init];
	criteriacount = 0;
    if ( self ) {
		predicateValue = [[NSString alloc] initWithString:query];
		criteriacount++;
    }
	
    return self;	
}

-(id)initWithCriterias:(NSMutableArray *)array option:(NSRTMPredicateAggregator)agg {
	self = [super init];
	criteriacount = 0;
	NSString *aggregator;
    if ( self ) {
		if(agg == NSRTMPredicateAND) {
			aggregator = [NSString stringWithString:AND];
		} else {
			aggregator = [NSString stringWithString:OR];
		}
		NSEnumerator * enumerator = [array objectEnumerator];
		NSRTMCriteria *element;
		predicateValue = [NSString stringWithString:@"("];
		while(element = [enumerator nextObject])
		{
			if(criteriacount == 0) {
				predicateValue = [predicateValue stringByAppendingFormat:@"%@ ",[element getFormattedCriteria]];
			} else {
				predicateValue = [predicateValue stringByAppendingFormat:@"%@ %@ ",aggregator,[element getFormattedCriteria]];
			}
			criteriacount++;
		}
		predicateValue = [NSString stringWithString:@") "];
    }
	
    return self;
}

-(void)addCriteria:(NSRTMCriteria *)criteria option:(NSRTMPredicateAggregator)agg {
	NSString *aggregator;
	if(agg == NSRTMPredicateAND) {
		aggregator = [NSString stringWithString:AND];
	} else {
		aggregator = [NSString stringWithString:OR];
	}
	predicateValue = [predicateValue stringByAppendingFormat:@"%@ %@ ",aggregator,[criteria getFormattedCriteria]];
	criteriacount++;

}

-(void)addCriterias:(NSMutableArray *)array grouppedWith:(NSRTMPredicateAggregator)inagg option:(NSRTMPredicateAggregator)agg {
	NSString *aggregator;
	NSString *in_aggregator;
	if(inagg == NSRTMPredicateAND) {
		in_aggregator = [NSString stringWithString:AND];
	} else {
		in_aggregator = [NSString stringWithString:OR];
	}
	if(agg == NSRTMPredicateAND) {
		aggregator = [NSString stringWithString:AND];
	} else {
		aggregator = [NSString stringWithString:OR];
	}
	NSEnumerator * enumerator = [array objectEnumerator];
	NSRTMCriteria *element;
	predicateValue = [predicateValue stringByAppendingFormat:@"%@ (",aggregator];
	while(element = [enumerator nextObject])
	{
		predicateValue = [predicateValue stringByAppendingFormat:@"%@ %@ ",aggregator,[element getFormattedCriteria]];
		criteriacount++;
	}
	predicateValue = [NSString stringWithString:@") "];
}

-(void)addPredicate:(NSRTMPredicate *)predicate option:(NSRTMPredicateAggregator)agg {
	NSString *aggregator;
	if(agg == NSRTMPredicateAND) {
		aggregator = [NSString stringWithString:AND];
	} else {
		aggregator = [NSString stringWithString:OR];
	}
	predicateValue = [predicateValue stringByAppendingFormat:@"%@ %@ ",aggregator,[predicate formattedPredicate]];
	criteriacount = criteriacount + [predicate getCriteriaCount];
	
}

-(NSString *)formattedPredicate {
	return predicateValue;
}

-(int)getCriteriaCount {
	return criteriacount;
}
@end
